﻿// // ******************************************************************
// //       /\ /|       @file       ConfigDataCommandHandler.cs
// //       \ V/        @brief      await29
// //       | "")       @author     Await, 285283010@qq.com
// //       /  |
// //      /  \\        @Modified   2024-11-11 19:11
// //    *(__\_\        @Copyright  Copyright (c) , 2024-11-07 19:11 Await
// // ******************************************************************

using AutomaticSubtitles.Service.Application.Config.Commands;
using AutomaticSubtitles.Service.DataAccess.Model;
using AutomaticSubtitles.Service.Domain.Config;

namespace AutomaticSubtitles.Service.Application.Config;

public class ConfigDataCommandHandler(IConfigDataRepository repository) : BaseAutoService
{
    [EventHandler]
    public async Task Handle2Async(CreateConfigAddCommand command)
    {
        var countAsync = await repository.GetCountAsync(x => x.Name == command.data.name);
        if (countAsync > 0)
            throw new ArgumentException(ReturnMessage(Repeat));
        var config = new Auto_Config(BasicsHelper.GetId())
        {
            Name = command.data.name,
            Type = command.data.type,
            UserName = command.data.userName,
            Password = command.data.password,
            Token = command.data.token,
            ServerUrl = command.data.serverUrl
        };
        await repository.AddAsync(config);
        command.Result = 1;
    }

    [EventHandler]
    public async Task Hande(UpdateConfigTokenCommand command)
    {
        var data = await repository.FindAsync(x => x.Id == command.id);
        IsDataNotExist(data);
        // if (data.Token != null && data.TokenExpireTimeDate > DateTime.Now && data.IsNormal == true)
        // {
        //     command.Result = data.Token;
        //     return;
        // }

        var userId = await repository.UpdateTokenAsync(data);
        if (userId == null)
        {
            data.IsNormal = false;
            await repository.UpdateAsync(data);
            throw new Exception(ReturnMessage(TokenError));
        }

        // data.Token = tokens.Value.Item1;
        data.UserId = userId;
        data.IsNormal = true;
        // data.TokenExpireTimeDate = DateTime.Now.AddMinutes(60); //设置一个小时的过期时间
        await repository.UpdateAsync(data);
        command.Result = userId;
    }

    [EventHandler]
    public async Task HandleAsync(UpdateConfigAddCommand command)
    {
        var countAsync = await repository.GetCountAsync(x => x.Name == command.data.name && x.Id != command.data.id);
        if (countAsync > 0)
            throw new ArgumentException("该配置已存在");
        var findAsync = await repository.FindAsync(x => x.Id == command.data.id) ?? throw new ArgumentException(ReturnMessage(NoExistent));
        findAsync.Name = command.data.name;
        findAsync.Type = command.data.type;
        findAsync.UserName = command.data.userName;
        findAsync.Password = command.data.password;
        findAsync.ServerUrl = command.data.serverUrl;
        findAsync.Token = command.data.token;

        await repository.UpdateAsync(findAsync);
        command.Result = 1;
    }

    [EventHandler]
    public async Task HandleAsync(DeleteConfigAddCommand command)
    {
        var findAsync = await repository.FindAsync(x => x.Id == command.id) ?? throw new ArgumentException(ReturnMessage(NoExistent));
        // await repository.RemoveAsync(command.id);
        await repository.RemoveAsync(findAsync);
        command.Result = 1;
    }
}